package com.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.data.DataBase;
import com.entity.Student;

/**
 * 学生的业务
 * 
 * @author YH
 *
 */
public class StudentService {
	
	
	static {
		/*//默认添加几个学生的信息
		Student student=new Student();
		student.setUsername("stu");
		student.setPassword("stu");
		student.setSex(1);
		student.setName("stu");
		student.setCreateDate(new Date());
		student.setAge(20);
		student.setAddress("address");
		
		//默认添加几个学生的信息
		Student student1=new Student();
		student1.setUsername("stu1");
		student1.setPassword("stu1");
		student1.setSex(1);
		student1.setName("stu1");
		student1.setCreateDate(new Date());
		student1.setAge(20);
		student1.setAddress("address1");
		
		//默认添加几个学生的信息
		Student student2=new Student();
		student2.setUsername("stu2");
		student2.setPassword("stu2");
		student2.setSex(1);
		student2.setName("stu2");
		student2.setCreateDate(new Date());
		student2.setAge(20);
		student2.setAddress("address2");
		
		DataBase.getStudents().put(student.getUsername(), student);
		DataBase.getStudents().put(student1.getUsername(), student1);
		DataBase.getStudents().put(student2.getUsername(), student2);*/
	}
	
	
	// 添加
	public static boolean add(Student student) {
		if(DataBase.getStudents().containsKey(student.getUsername())){
			return false;
		}
		else{
			DataBase.getStudents().put(student.getUsername(), student);
			return true;
		}
		
	}

	// 删除
	public static void del(String username) {
		DataBase.getStudents().remove(username);
	}

	// 改
	public static void update(Student student) {
		DataBase.getStudents().put(student.getUsername(), student);
	}

	// 查所有
	public static List<Student> find() {
		Map<String, Student> stus = DataBase.getStudents();
		Set<String> kSet = stus.keySet();
		Object[] objs = kSet.toArray();
		List<Student> list = new ArrayList<Student>();
		for (int i = 0; i < objs.length; i++) {
			String sid = (String) (objs[i]);
			list.add(stus.get(sid));
		}
		return list;
	}

	// 查一个
	public static Student findByUserName(String username) {
		return DataBase.getStudents().get(username);
	}

	// 登录
	public static boolean login(String username,String password){
		Student stu=DataBase.getStudents().get(username);
		try {
			if(password.equals(stu.getPassword())){
				//登录成功
				DataBase.getRequest().put("login", stu);
				return true;
			}
			else{
				//登录失败
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		
	}
}
